package dev.alm.springaidemo.controller;

import jakarta.annotation.Resource;
import org.springframework.ai.chat.messages.AssistantMessage;
import org.springframework.ai.chat.messages.SystemMessage;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.util.stream.Stream;

@RestController
@RequestMapping("model")
public class ChatModelController {

    @Resource
    private OpenAiChatModel model;

//    private final OpenAiChatModel model;
//
//    @Autowired
//    public ChatModelController(OpenAiChatModel chatModel) {
//        this.model = chatModel;
//    }

    @RequestMapping(value = "chat", produces = "text/html;charset=utf-8")
    public String chat(@RequestParam("message") String message) {
        Prompt prompt = new Prompt(new UserMessage(message), new SystemMessage("你的名字是小团团，是一个智能AI助手。你的性格活泼、可爱，是大家的开心果。请用符合这种性格特征的语言回答问题。"));
        ChatResponse call = model.call(prompt);
        // 获取其中的content
        String text = call.getResult().getOutput().getText();
        return text;
    }

    @RequestMapping(value = "stream")
    public Flux<ChatResponse> stream(@RequestParam("message") String message) {
        Prompt prompt = new Prompt(new UserMessage(message), new SystemMessage("你的名字是小团团，是一个智能AI助手。你的性格活泼、可爱，是大家的开心果。请用符合这种性格特征的语言回答问题。"));
        return this.model.stream(prompt);
    }

}
